<?php
/**
 * Created by PhpStorm.
 * User: hjlan
 * Date: 2016-8-8
 * Time: 13:51:03
 */

namespace App\Http\Controllers\Backstage;
use App\Http\Controllers\Controller;
use App\Services\ImgServices\ImgTool;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Response;
class ImgControllers extends Controller
{
    private $imgTool;
    public function __construct(ImgTool $imgTool)
    {
        $this->imgTool=$imgTool;
    }

    /***
     * 保存图片
     * @param Request $request
     * @return mixed
     */
    public function save(Request $request){
        $v=array(
            'image'=>'required|image'
        );
        $image=$request->file('image');
        $validator=ImgTool::store($request,$v);
        if($validator->fails()){
            if($validator->errors()->first('image')){
                $result['state']=-1;
                $result['message']='图片格式错误';
                $result['image_url']='';
            }
            if(isset($result)){
                return response()->json($result);
            }
        }
        $img_path=$this->imgTool->imgSave($image,2);
        if(!empty($img_path)){
            $img_url=ImgTool::getImgUrlByImgPath($img_path);
        }
        if(isset($img_url)&&!empty($img_url)){
            $result['state']=1;
            $result['message']='成功';
            $result['image_url']=$img_url;
        }else{
            $result['state']=-1;
            $result['message']='失败';
            $result['image_url']='';
        }
        return response()->json($result);
    }
}